package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.generator.entity.CommodityEntity;
import io.renren.modules.generator.vo.InformationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.generator.entity.InformationEntity;
import io.renren.modules.generator.service.InformationService;
import io.renren.modules.generator.service.CommodityService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 对应用户该表为购物车，对应外卖小哥该表为订单列表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-04-05 12:40:56
 */
@RestController
@RequestMapping("generator/information")
@Api("购物车/订单列表")
public class InformationController {
    @Autowired
    private InformationService informationService;
    @Autowired
    private CommodityService commodityService;

    /**
     * 列表
     */
    @ApiOperation("用户详情列表(购物车/订单)")
    @GetMapping("/list")
    @RequiresPermissions("generator:information:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = informationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表ById
     */
    @ApiOperation("用户详情列表(购物车/订单)")
    @GetMapping("/listById")
    @RequiresPermissions("generator:information:list")
    public R listById(@RequestParam Map<String, Object> params){
        PageUtils page = informationService.queryInformationByUserId(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("用户详情(购物车/订单)")
    @GetMapping("/info/{id}")
    @RequiresPermissions("generator:information:info")
    public R info(@PathVariable("id") Integer id){
		InformationEntity information = informationService.getById(id);

        return R.ok().put("information", information);
    }

    /**
     * 根据订单id查询信息
     */
    @ApiOperation("用户详情(购物车/订单)")
    @GetMapping("information/info/{informationId}")
    @RequiresPermissions("generator:information:info")
    public R information_info(@PathVariable("informationId") String id){
        List<InformationEntity> information = informationService.list(new QueryWrapper<InformationEntity>().eq("information_id", id));

        for (InformationEntity entity : information) {
            String commodityId = entity.getCommodityId();
            CommodityEntity commodityEntity = commodityService.getById(commodityId);
            entity.setCommodityEntity(commodityEntity);
        }

        return R.ok().put("information", information);
    }

    /**
     * 根据userId查询订单信息
     */
    @ApiOperation("根据用户id查询订单详情(购物车/订单)")
    @GetMapping("/info/{userId}")
    @RequiresPermissions("generator:information:info")
    public R infoByUserId(@PathVariable("userId") Integer userId){
        InformationEntity information = informationService.getById(userId);

        return R.ok().put("information", information);
    }

    /**
     * 用户评分
     */
    @ApiOperation("用户评分。需要带上商品id及分数")
    @PostMapping("/grade")
    @RequiresPermissions("generator:information:save")
    public R grade(@RequestBody InformationVo informationVo){
        informationService.gradeOrder(informationVo);

        return R.ok();
    }

    /**
     * 保存
     */
    @ApiOperation("用户详情(购物车/订单)")
    @PostMapping("/save")
    @RequiresPermissions("generator:information:save")
    public R save(@RequestBody InformationVo informationVo){
		String informationId = informationService.addOrder(informationVo);

        // 返回订单Id
        return R.ok().put("information_id", informationId);
    }

    /**
     * 修改
     */
    @ApiOperation("修改用户详情(购物车/订单)")
    @PostMapping("/update")
    @RequiresPermissions("generator:information:update")
    public R update(@RequestBody InformationEntity information){
		informationService.updateById(information);

        return R.ok();
    }

    /**
     * 修改订单状态
     */
    @ApiOperation("修改订单状态。需要订单id跟目标态，如果是骑手需要加上currentUserId")
    @PostMapping("/change")
    @RequiresPermissions("generator:information:update")
    public R change(@RequestBody InformationVo informationVo){
        informationService.changeInformation(informationVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除用户详情(购物车/订单)")
    @PostMapping("/delete")
    @RequiresPermissions("generator:information:delete")
    public R delete(@RequestBody Integer[] ids){
		informationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
